<template>
  <div class="app-container">
<!--    <el-tag type="info" style="margin-right: 30px; margin-bottom: 20px; width: 100%; height: 45px; padding: 10px; font-size: 14px; font-weight: bold">会员等级列表</el-tag>-->
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" placeholder="请选择" clearable >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
<!--        <el-button-->
<!--          type="danger"-->
<!--          @click="deleteHandle()"-->
<!--          :disabled="dataListSelections.length <= 0"-->
<!--        >批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="收货人 姓名">
              <span>{{ props.row.receiverName }}</span>
            </el-form-item>
            <el-form-item label="收货人 电话">
              <span>{{ props.row.receiverPhone }}</span>
            </el-form-item>
            <el-form-item label="收货人 邮编">
              <span>{{ props.row.receiverPostCode }}</span>
            </el-form-item>
            <el-form-item label="省份 / 直辖市">
              <span>{{ props.row.receiverProvince }}</span>
            </el-form-item>
            <el-form-item label="城      市">
              <span>{{ props.row.receiverCity }}</span>
            </el-form-item>
            <el-form-item label="区">
              <span>{{ props.row.receiverRegion }}</span>
            </el-form-item>
            <el-form-item label="详 细 地 址">
              <span>{{ props.row.receiverDetailAddress }}</span>
            </el-form-item>
            <el-form-item label="运 费 金 额">
              <span>￥{{ props.row.freightAmount }}</span>
            </el-form-item>
            <el-form-item label="促销优惠金额">
              <span>￥{{ props.row.promotionAmount }}</span>
            </el-form-item>
            <el-form-item label="积分抵扣金额">
              <span>￥{{ props.row.integrationAmount }}</span>
            </el-form-item>
            <el-form-item label="优惠券抵扣金额">
              <span>￥{{ props.row.couponAmount }}</span>
            </el-form-item>
            <el-form-item label="后台调整订单使用折扣">
              <span>￥{{ props.row.discountAmount }}</span>
            </el-form-item>
            <el-form-item label="订单备注">
              <span>{{ props.row.note }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
<!--      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
      <el-table-column prop="id" header-align="center" align="center" label="id" width="200"></el-table-column>
      <el-table-column prop="memberUsername" header-align="center" align="center" label="用户名"></el-table-column>
      <el-table-column prop="orderSn" header-align="center" align="center" label="订单号" width="165"></el-table-column>
      <el-table-column prop="totalAmount" header-align="center" align="center" label="订单总额"></el-table-column>
      <el-table-column prop="payAmount" header-align="center" align="center" label="应付总额"></el-table-column>
      <el-table-column prop="sourceType" header-align="center" align="center" label="订单来源">
        <template slot-scope="scope">
          PC订单
        </template>
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="订单状态">
        <template slot-scope="scope">
          <el-tag type="success" effect="plain" v-if="scope.row.status === 0">待付款</el-tag>
          <el-tag type="info" effect="dark" v-if="scope.row.status === 1">待发货</el-tag>
          <el-tag type="" effect="dark" v-if="scope.row.status === 2">已发货</el-tag>
          <el-tag type="success" effect="dark" v-if="scope.row.status === 3">已完成</el-tag>
          <el-tag type="warning" effect="dark" v-if="scope.row.status === 4">已关闭</el-tag>
          <el-tag type="danger" effect="dark" v-if="scope.row.status === 5">无效订单</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="deliveryTime" header-align="center" align="center" label="发货时间" width="200">
        <template slot-scope="scope">
          {{ scope.row.deliveryTime ? renderTime(scope.row.deliveryTime) : '' }}
        </template>
      </el-table-column>
      <el-table-column prop="confirmStatus" header-align="center" align="center" label="确认收货状态" width="200">
        <template slot-scope="scope">
          {{ scope.row.confirmStatus ? renderTime(scope.row.confirmStatus) : '' }}
        </template>
      </el-table-column>
      <el-table-column prop="receiveTime" header-align="center" align="center" label="确认收货时间" width="200">
        <template slot-scope="scope">
          {{ scope.row.receiveTime ? renderTime(scope.row.receiveTime) : '' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间" width="200">
        <template slot-scope="scope">
          {{ scope.row.createTime ? renderTime(scope.row.createTime) : '' }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="getOrderItem(scope.row.orderSn)">订单项</el-button>
          <el-dropdown>
            <el-button type="text" size="small">更改状态<i class="el-icon-caret-bottom"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleChangeStatus(scope.row.id, 1)">更改待发货</el-dropdown-item>
              <el-dropdown-item @click.native="handleChangeStatus(scope.row.id, 2)">更改已发货</el-dropdown-item>
              <el-dropdown-item @click.native="handleChangeStatus(scope.row.id, 3)">更改已完成</el-dropdown-item>
              <el-dropdown-item @click.native="handleChangeStatus(scope.row.id, 4)">更改已关闭</el-dropdown-item>
              <el-dropdown-item @click.native="handleChangeStatus(scope.row.id, 5)">更改无效订单</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 10px"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <userOrderItem ref="userOrderItem" @refreshDataList="getDataList"></userOrderItem>


  </div>
</template>

<script>
import userOrderItem from "./userOrder-item";
import { list, delMultiple, changeStatus } from '@/api/order/order'

export default {
  data() {
    return {
      dataForm: {
        key: "",
        status: ""
      },
      options: [
        {
          label: '待付款',
          value: 0
        },
        {
          label: '待发货',
          value: 1
        },
        {
          label: '已发货',
          value: 2
        },
        {
          label: '已完成',
          value: 3
        },
        {
          label: '已关闭',
          value: 4
        },
        {
          label: '无效订单',
          value: 5
        }
      ],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    userOrderItem
  },
  created() {
    this.getDataList();
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      list({
        'page': this.pageIndex,
        'limit': this.pageSize,
        'key': this.dataForm.key,
        'status': this.dataForm.status
      }).then((data) => {
        this.dataList = data.page.list
        this.totalPage = data.page.totalCount
        this.dataListLoading = false
      }).catch(()=>{
        this.dataList = []
        this.totalPage = 0
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id, name) {
      const names = []
      const ids = []
      if (id) {
        ids.push(id)
        names.push(' 【' + name + '】 ')
      } else {
        this.dataListSelections.map(item => {
          ids.push(item.id)
          names.push(' 【' + item.name + '】 ')
          return item.id
        })
      }

      this.$confirm(
        `确定对 ${names.join(',')} 进行${id ? '删除' : '批量删除'}操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        delMultiple(ids)
        .then(() => {
          this.$message({
            message: "操作成功",
            type: "success",
          })
          this.getDataList()
        }).catch(()=>{})
      }).catch(()=>{})
    },
    handleChangeStatus(id, status) {
      changeStatus(id, status)
        .then(() => {
          this.getDataList()
          this.$message({
            message: "已更改",
            type: "success",
          })
        }).catch()
    },
    getOrderItem(id) {
      this.$refs.userOrderItem.init(id);
    },
    renderTime(date) {
      const dateee = new Date(date).toJSON();
      return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    }
  }
};
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 120px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-left: 20px;
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
